#include "bigint.h"
#include "bigintptr.h"

BigIntPtr::BigIntPtr(BigInt * p)
	: storage_(new Storage(p))
{ }

BigIntPtr::~BigIntPtr(){
	storage_ -> dec();
	if ( storage_ -> counter() == 0){
		delete storage_;   
	}
}

BigInt & BigIntPtr::operator* () const{
	return *(storage_ -> ptr_);
}

BigInt * BigIntPtr::operator-> () const{
	return storage_ -> ptr_;
}               

BigIntPtr::BigIntPtr(BigIntPtr const & p)
	: storage_(p.storage_)
{ 
	storage_->inc();
}

void BigIntPtr::operator= (BigInt * p) {
	storage_ -> dec();
	if (storage_ -> counter() == 0){
		delete storage_;
	}
	storage_ = new Storage(p);
}

bool operator == (BigIntPtr const bp1, BigIntPtr const bp2){
	if (bp1.storage() -> ptr_ == bp2.storage() -> ptr_){
		return true;
	}
	return false;
}

bool operator != (BigIntPtr const bp1, BigIntPtr const bp2){
	return !(bp1 == bp2);
}

bool operator > (BigIntPtr const bp1, BigIntPtr const bp2){
	return bp1.storage() -> ptr_ > bp2.storage() -> ptr_;
}

bool operator < (BigIntPtr const bp1, BigIntPtr const bp2){
	return !(bp1 > bp2);
}

bool operator >= (BigIntPtr const bp1, BigIntPtr const bp2){
	return !(bp1 < bp2);
}

bool operator <= (BigIntPtr const bp1, BigIntPtr const bp2){
	return !(bp1 > bp2);
}

Storage * BigIntPtr::storage() const{
	return storage_;
}
